<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/wms/warehouseLocation/warehouseLocationList' }">库位信息列表</el-breadcrumb-item>
      <el-breadcrumb-item>库位信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">

          <el-row>
            <el-col :span="12">
              <el-form-item label="所属仓库" prop="warehouseInfoId">
                <el-select v-model="editForm.warehouseInfoId">
                  <el-option v-for="(item, index) in warehouses" :key="index" :label="item.code + '-' + item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属库区" prop="warehouseAreaId">
                <el-select v-model="editForm.warehouseAreaId">
                  <el-option v-for="(item, index) in warehouseAreas" :key="index" :label="item.code + '-' + item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库位编码" prop="code">
                <el-input v-model="editForm.code" :disabled="editForm.id ? true : false" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库位名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { viewWarehouseLocation, saveWarehouseLocation, updateWarehouseLocation } from 'src/api/wms/warehouseLocation'
import { listWarehouseInfo } from 'src/api/wms/warehouseInfo'
import { listWarehouseArea } from 'src/api/wms/warehouseArea'

//查询仓库/库区
const warehouses = ref<Array<any>>([])
listWarehouseInfo().then(({ data }) => {
  warehouses.value = data
})

const warehouseAreas = ref<Array<any>>([])
const initWarehouseArea = (warehouseInfoId: string) => {
  listWarehouseArea({ warehouseInfoId }).then(({ data }) => {
    warehouseAreas.value = data
  })
}

// 定义表单数据
const editFormRef = ref<FormInstance>()
const editForm = reactive({
  id: '',
  warehouseInfoId: '',
  warehouseCategory: '',
  warehouseProperty: '',
  warehouseInfoCode: '',
  warehouseInfoName: '',
  warehouseAreaId: '',
  warehouseAreaCode: '',
  warehouseAreaName: '',
  code: '',
  name: '',
  remark: ''
})

const editFormRules = {
  warehouseInfoId: [
    { required: true, message: '请选择所属仓库', trigger: ['blur', 'change'] }
  ],
  warehouseAreaId: [
    { required: true, message: '请选择所属库区', trigger: ['blur', 'change'] }
  ],
  code: [
    { required: true, message: '请输入库位编码', trigger: ['blur'] }
  ],
  name: [
    { required: true, message: '请输入库位名称', trigger: ['blur'] }
  ]
}

// 修改时填充表单数据
const route = useRoute()
if (route.query.id) {
  const id = route.query.id as string
  viewWarehouseLocation(id).then(({ data }) => {
    Object.assign(editForm, data)
  })
}

//监听所属仓库/库区
watch(() => editForm.warehouseInfoId, (newVal: string) => {
  if (!newVal) {
    return
  }
  const warehouse = warehouses.value.find((item) => item.id == newVal)
  if (!warehouse) {
    return
  }
  editForm.warehouseInfoCode = warehouse.code
  editForm.warehouseInfoName = warehouse.name
  editForm.warehouseCategory = warehouse.category
  initWarehouseArea(newVal)
})

watch(() => editForm.warehouseAreaId, (newVal: string) => {
  if (!newVal) {
    return
  }
  const warehouseArea = warehouseAreas.value.find((item) => item.id == newVal)
  if (!warehouseArea) {
    return
  }
  editForm.warehouseAreaCode = warehouseArea.code
  editForm.warehouseAreaName = warehouseArea.name
  editForm.warehouseProperty = warehouseArea.property
})

// 提交
const submit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveWarehouseLocation(editForm).then(() => {
        cancel()
      })
    } else {
      updateWarehouseLocation(editForm).then(() => {
        cancel()
      })
    }
  })
}

// 取消
const router = useRouter()
const cancel = () => {
  router.push('/wms/warehouseLocation/warehouseLocationList')
}

</script>

<style lang="scss" scoped></style>

